//
//  DDToolView.m
//  QQZone
//
//  Created by 树清吴 on 2017/10/16.
//  Copyright © 2017年 shuQingWu. All rights reserved.
//

#import "DDToolView.h"
#import "DDButton.h"

@interface DDToolView ()

@property (nonatomic, strong) NSMutableArray *btnArray;
@property (nonatomic, weak) UIButton *lastBtn;

@end

@implementation DDToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = DDGrayColor;
        
        [self addButtonWithTitle:@"好友" image:@"tab_bar_e_album_icon"];
        [self addButtonWithTitle:@"与我相关" image:@"tab_bar_feed_icon"];
        [self addButtonWithTitle:@"照片墙" image:@"tab_bar_friend_icon"];
        [self addButtonWithTitle:@"电子相框" image:@"tab_bar_passive_feed_icon"];
        [self addButtonWithTitle:@"设置" image:@"tab_bar_e_more_icon"];
        [self addButtonWithTitle:@"好友" image:@"tab_bar_e_album_icon"];

        
    }
    return self;
}

- (void)btnClick:(DDButton *)sender{
    
    self.lastBtn.enabled = YES;
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"DDToolViewButtonClickNotification" object:self userInfo:@{@"tag": @(sender.tag)}];

    sender.enabled = NO;
    self.lastBtn = sender;
    
}

/// 添加按钮到view上面，并且设置tag值
- (void)addButtonWithTitle:(NSString *)title image:(NSString *)image{
    DDButton *btn = [DDButton buttonWithTitle:title image:image highlighted:nil background:nil backgroundHighlighted:nil target:self action:@selector(btnClick:)];
    [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_separate_selected_bg"] forState:UIControlStateDisabled];
    btn.tag = self.subviews.count;
    [self addSubview:btn];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    NSInteger count = self.subviews.count;
    for (int i = 0; i < count; i ++) {
        DDButton *view = self.subviews[i];
        view.width = self.width;
        view.x = 0;
        if (DDPortrait) {
            view.height = view.width;
            view.titleLabel.width = 0;
            view.titleLabel.height = 0;
        }else{
            view.height = self.height / count;
            view.titleLabel.hidden = NO;
        }
        view.y = view.height * i;
    }

    
}

#pragma mark -- 懒加载
- (NSMutableArray *)btnArray{
    if (_btnArray == nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

@end
